#include "hNetworkReply.h"

HNetworkReply::HNetworkReply(QObject *parent) : QObject(parent)
{
    finished=false;
    connect(&outTimer,SIGNAL(timeout()),this,SLOT(reply_TimeOut_Slot()));
}

void HNetworkReply::reply_TimeOut_Slot(){
    const char *dc="{\"result\":\"FAIL\",\"data\":[{\"reason\":\"服务器响应超时\"}]}";
    QJsonDocument rsp(QJsonDocument::fromJson(QByteArray(dc)));
    emit reply_rsp(reply_URL,rsp);
 //   this->deleteLater();
}
HNetworkReply::~HNetworkReply(){
    outTimer.stop();
    if(reply != nullptr){
        delete reply;
    }
}
void HNetworkReply::reply_setNetworkReply(QNetworkReply *reply){
    if(reply){
        this->reply = reply;
        reply_RawURL = this->reply->request().url().toString();
        int s=reply_RawURL.indexOf(QString(HTTP_URLPREFIX),0);
        if(s!=0){
             reply_URL=reply_RawURL.mid(s+10,reply_RawURL.size());
             connect(reply,SIGNAL(finished()),this,SLOT(reply_Finish_Slot()));
             outTimer.start(TimeOutInterval);
        }else{
            const char *dc="{\"result\":\"FAIL\",\"data\":[{\"reason\":\"URL错误\"}]}";
            QJsonDocument rsp(QJsonDocument::fromJson(QByteArray(dc)));
            emit reply_rsp(reply_URL,rsp);
        }
    }else{
         const char *dc="{\"result\":\"FAIL\",\"data\":[{\"reason\":\"请求失败\"}]}";
         QJsonDocument rsp(QJsonDocument::fromJson(QByteArray(dc)));
         emit reply_rsp(reply_URL,rsp);
    }
}
void HNetworkReply::reply_Finish_Slot(){
    outTimer.stop();
      if(this->reply->bytesAvailable()){
          reply_Pack.append(this->reply->readAll());
      }
      int  reply_Pack_len = reply_Pack.size();
      if(reply_Pack_len>0 && this->reply->error() == QNetworkReply::NoError){
          int t,s,e;
          t=0;
          while(t<reply_Pack_len){
              if(reply_Pack[t]=='['){
                  s=t+1;
                  break;
              }else{
                  t++;
              }
          }
          t=reply_Pack_len;
          while(t>0){
              if(reply_Pack[t]==']' && reply_Pack[t-1]=='}'){
                  e=t;
                  break;
              }else{
                  t--;
              }
          }
          reply_Pack.remove(e,reply_Pack_len-e);
          reply_Pack.remove(0,s);
          QByteArray rspBytes;
          for(t=s;t<=e;t++){
              rspBytes.append(reply_Pack[t]);
          }
          QJsonDocument document=QJsonDocument::fromJson(reply_Pack);
          QJsonObject jsonObject = document.object();
          QJsonArray jsonData = jsonObject["data"].toArray();
          QJsonObject levelObject = jsonData[0].toObject();
          QString data=levelObject["reason"].toString();
          if(data.contains("用户已退出")){
              PRTLOG()<<"the receive content is user exit";
          }
          if(reply_RawURL.contains(HTTP_URLPREFIX)){
              emit reply_rsp(reply_URL,QJsonDocument::fromJson(reply_Pack));
          }else{
              PRTLOG()<<"URL:"<<reply_RawURL<<"is invalid";
          }
      }else{
          const char *dc="{\"result\":\"FAIL\",\"data\":[{\"reason\":\"服务器错误\"}]}";
          emit reply_rsp(reply_URL,QJsonDocument::fromJson(QByteArray(dc)));
      }
      finished=true;
      this->deleteLater();
}

